package sein.android.hopenote;

import java.util.ArrayList;

import sein.android.hopenote.Const.HopeNoteDb;
import android.app.Activity;
import android.content.ComponentName;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v4.widget.SimpleCursorAdapter;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.TextView;

public class Item_Set extends Activity {
	
	private DBAdapter HnopenHelper;
	private SQLiteDatabase sdb;	
	

	private	Button btn_conf;
	private	Spinner spin_group;
	private ListView lst_itemview;
	private	Cursor cur_group, cur_item;
	private	ArrayList<String> lst_group, lst_item, lst_unit, lst_cnt;
	private	ArrayAdapter<String> adapter, adapter_item;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.item_set);
		
			btn_conf = (Button)findViewById(R.id.btn_conf);		
			spin_group = (Spinner)findViewById(R.id.sp_category);
			lst_itemview = (ListView)findViewById(R.id.lst_itemview);
	         
	        HnopenHelper = new DBAdapter(this);
	        sdb = HnopenHelper.getReadableDatabase();
	     
	     
	      //set group
	        cur_group = sdb.query(true,HopeNoteDb.TB_ITEM, new String[] {HopeNoteDb.COL_GROUP}, null, null, null, null, null,null);
	        cur_group.moveToFirst();
	        lst_group = new ArrayList<String>();
	        for(int i = 0; i < cur_group.getCount(); i++){
	        	lst_group.add(cur_group.getString(0));
	        	cur_group.moveToNext();
	        }
	        cur_group.close();
	        spin_group.setPrompt("카테고리 선택");
	        adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, lst_group);
	        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
	        spin_group.setAdapter(adapter);
	        
	        
	      //set item & unit
	        String qWhere = HopeNoteDb.COL_GROUP + " = '" + spin_group.getSelectedItem().toString() + "'";
	        cur_item = sdb.query(HopeNoteDb.TB_ITEM, new String[] {HopeNoteDb.COL_ITEM,HopeNoteDb.COL_UNIT}, qWhere, null, null, null, null);
	        cur_item.moveToFirst();
	        lst_item = new ArrayList<String>();
	        lst_unit = new ArrayList<String>();
	        lst_cnt = new ArrayList<String>();
	        for(int i = 0; i < cur_item.getCount(); i++){
	        	lst_item.add(cur_item.getString(0));
	        	lst_unit.add(cur_item.getString(1));
	        	lst_cnt.add(cur_item.getString(2));
	        	cur_item.moveToNext();
	        }
	        cur_item.close();
	        //adapter_item = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_checked, lst_item);
	        //adapter_item.setDropDownViewResource(android.R.layout.simple_list_item_checked);
	        
	        
	        //여기에 라디오 박스 adapter 연결해주세요~~~ adapter_item 쓰셔서 연결해 주시면 되요~~~
	        ArrayAdapter<String> Adapter;
	        Adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_multiple_choice,lst_unit);
	        
	        ListView lst_itemview = (ListView)findViewById(R.id.lst_itemview);
	        lst_itemview.setAdapter(Adapter);
	        
	
	        //group Selection Event
		spin_group.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
	
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				// TODO Auto-generated method stub
				ChangeSpinItem();
			}
	
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub
				
			}
		});
	
		btn_conf.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent();
				intent.putExtra(HopeNoteDb.EXTRA_SEARCH_ITEM,"NOTE_ITEM" );
				intent.putExtra(HopeNoteDb.EXTRA_SEARCH_UNIT,"NOTE_ITEM" );
				intent.putExtra(HopeNoteDb.EXTRA_SEARCH_CNT,"NOTE_ITEM" );
				setResult(RESULT_OK,intent);
				finish();
			}
		});
	}
		
		private void ChangeSpinItem() {
			//set item & unit
	        String qWhere = HopeNoteDb.COL_GROUP + " = '" + spin_group.getSelectedItem().toString() + "'";
	        cur_item = sdb.query(HopeNoteDb.TB_ITEM, new String[] {HopeNoteDb.COL_ITEM,HopeNoteDb.COL_UNIT,HopeNoteDb.COL_CNT}, qWhere, null, null, null, null);
	        cur_item.moveToFirst();
	        lst_item.clear();
	        lst_unit.clear();
	        lst_cnt.clear();
	        for(int i = 0; i < cur_item.getCount(); i++){
	        	lst_item.add(cur_item.getString(0));
	            lst_unit.add(cur_item.getString(1));
	            lst_cnt.add(cur_item.getString(2));
	        	cur_item.moveToNext();
	        }
	        adapter_item.notifyDataSetChanged();
	        cur_item.close();
	      
		}
		
}

